package com.company.project.configurer;


import com.company.project.resolver.MyExceptionResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * Spring MVC 配置
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private final Logger logger = LoggerFactory.getLogger(WebMvcConfig.class);

    @Value("${spring.profiles.active}")
    private String env;//当前激活的配置文件

////使用spring-boot默认的mappingjackjson也ok
//    /**
//     * 消息内容转换配置
//     * 配置fastJson返回json转换
//     * @param converters
//     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//
//        StringHttpMessageConverter stringHttpMessageConverter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
//
//        //创建fastJson消息转换器
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//        //创建配置类
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        //修改配置返回内容的过滤
//        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature.DisableCircularReferenceDetect,
//                SerializerFeature.WriteMapNullValue,
//                SerializerFeature.WriteNullStringAsEmpty
//        );
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//        //将fastjson添加到视图消息转换器列表内
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//
//
//        converters.add(stringHttpMessageConverter);
//        converters.add(fastConverter);
//    }


//    //添加拦截器
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//
//        //接口签名认证拦截器，该签名认证比较简单，实际项目中可以使用Json Web Token或其他更好的方式替代。
//        if (!"dev".equals(env)) { //开发环境忽略签名认证
//            registry.addInterceptor(new SignatureInterceptor());
//        }
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/mystatic/**").addResourceLocations("classpath:/mystatic/");
        registry.addResourceHandler("/**").addResourceLocations("/");
//        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
    }


    //解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //registry.addMapping("/**");
    }

    //统一异常处理
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers)
    {
        exceptionResolvers.add(new MyExceptionResolver());
    }

}
